<?php
/**
 * Created by yaoyu.
 * User: Administrator
 * Date: 2020/3/31
 * Time: 13:55
 */

namespace App\Admin\Controllers;


use App\Model\News;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Routing\Controller;

class NewsController extends Controller
{
    use HasResourceActions;

    /**
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content->header('新闻列表')
                       ->body($this->grid());
    }

    /**
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content->header('create')->body($this->form());
    }

    /**
     * @param $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('描述')
            ->body($this->form()->edit($id));
    }

    /**
     * @return Form
     */
   public function form()
   {
       $form = new Form(new News());

       $form->text('title','新闻标题');
       $form->text('author','发布人')->width('300px');
       $form->file('imgurl', '图片');
       $form->ckeditor('content','新闻内容');
       $form->display('id','新闻ID');

       return $form;
   }

    /**
     * 数据
     * @return Grid
     */
    public function grid()
    {
        $grid = new Grid(new News());

        $grid->id()->sortable();
        $grid->title('文章标题');
        $grid->created_at('创建时间');
        $grid->updated_at('修改时间');
        $grid->author('发布人');
        return $grid;
    }

    /**
     * @param $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content->header('详情')
                       ->body($this->detail($id));
    }

    /**
     * @param $id
     * @return Show
     */
    public function detail($id)
    {
        $show = new Show(News::findOrFail($id));

        $show->id('新闻ID');
        $show->title('新闻标题');
        $show->author('发布人');
        $show->imgurl('图片')->setEscape(false)->as(function($img_url){
            $path = env('APP_URL');
            $data='';
            return $data . "<img src='{$path}/upload/{$img_url}' width='140'/>";
        });
        $show->content('新闻内容');
        $show->created_at('创建时间');
        $show->updated_at('修改时间');

        return $show;
    }
}